<!--流程 分公司二审 -->
<template>
  <div>
    <Card :bordered="false">
      <Row>
        <Col span="12">二审</Col>
        <Col span="2"><Button @click="goReturn" type="text">返回</Button></Col>
        <Col span="2"><Button :disabled="tofStateOne" @click="firstInstance" type="text">一审</Button></Col>
        <Col span="2"><Button disabled type="text">二审</Button></Col>
        <Col span="2"><Button :disabled="tofStateThr" @click="firstTrial" type="text">初审</Button></Col>
        <Col span="2"><Button :disabled="tofStateFou" @click="reExamine" type="text">复审</Button></Col>
        <Col span="2"><Button :disabled="tofStateFiv" @click="lastInstance" type="text">终审</Button></Col>
      </Row>
      <div class="ivu-table-wrapper">
        <div class="ivu-table ivu-table-default ivu-table-border">
          <div class="ivu-table-header">
            <table cellspacing="0" cellpadding="0" border="1" width="100%">
              <thead>
                <th style="text-align:center">
                  <div class="ivu-table-cell">二审修改单</div>
                </th>
              </thead>
            </table>
          </div>
          <div class="ivu-table-body">
            <table cellspacing="0" cellpadding="0" border="0" width="100%">
              <tbody class="ivu-table-tbody">
                <tr class="ivu-table-row">
                  <td>
                    <div class="ivu-table-cell">二审人员</div>
                  </td>
                  <td>
                    <div class="ivu-table-cell">
                      {{info.Audit}}
                    </div>
                  </td>
                  <td>
                    <div class="ivu-table-cell">二审时间</div>
                  </td>
                  <td>
                    <div class="ivu-table-cell">
                      <p v-for='(item,index) in info.AuditTime' :key='index'>{{item}}</p>
                    </div>
                  </td>
                </tr>
                <tr class="ivu-table-row">
                  <td>
                    <div class="ivu-table-cell">修改人员</div>
                  </td>
                  <td>
                    <div class="ivu-table-cell"><template>
                        <Select v-model="info.Modifier" :disabled="viewOnlyState" @on-change="chengeXgry" :label-in-value="true">
                          <Option v-for="(item,index) in xzRy" :value="item.Value" :label="item.Label" :key="index">{{
                            item.Label }}</Option>
                          <!-- <Option v-for="item in xzRy" :value="item.Staff_name" :key="item.staffs_name">{{ item.Staff_name }}</Option> -->
                        </Select>
                      </template></div>
                  </td>
                  <td>
                    <div class="ivu-table-cell">修改完成时间</div>
                  </td>
                  <td>
                    <div class="ivu-table-cell">
                      <p v-for="(item,index) in info.ChangeTime" :key=index>{{item}}</p>
                    </div>
                  </td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>
      </div>
      <template>
        <Table border highlight-row @on-row-click="getIndex" :columns="columns1" :data="info.data" style="margin-top:10px;"></Table>
        <!-- <div style="border:1px solid #e8eaec;border-top:0;text-align:center;padding:10px 0;">
          <Button type="primary" :disabled="addBtton" style="margin-right:12px;width:80px;" @click="inputAdd('data')">添加</Button>
          <Button type="error" class="btnwidth" @click="inputRemove('data')">删除</Button>
        </div> -->
      </template>
      <div class="ivu-table-wrapper" style="margin-top:10px;">
        <div class="ivu-table ivu-table-default ivu-table-border">
          <div class="ivu-table-header">
            <table cellspacing="0" cellpadding="0" border="0" width="100%">
              <thead>
                <th style="text-align:center">
                  <div class="ivu-table-cell">报告（正文、附图附件附表）</div>
                </th>
              </thead>
            </table>
          </div>
          <div class="ivu-table-body">
            <table cellspacing="0" cellpadding="0" border="0" width="100%">
              <tbody class="ivu-table-tbody">
                <tr class="ivu-table-row">
                  <td>
                    <span style="padding-left:15px;">参考意见文件：</span> 
                    <Button :disabled="!ProjectEnvOverview?true:false" :to="ProjectEnvOverview" target='_blank'>{{xiazai}}</Button>
                    <div class="ivu-table-cell" style="display:inline;float:right" >
                      <template>
                        <Upload with-credentials :before-upload="upLoadFun" :data='imgUpload' :action="shishi" style="display:inline-block;" ref="uploadButton">
                          <Button icon="ios-cloud-upload-outline" :disabled='!stats'>上传</Button>
                        </Upload>
                      </template>
                    </div>
                    <div style="float:right;vertical-align:middle">
                      <span style="line-height:32px;vertical-align:middle">请上传压缩包文件</span>
                    </div>
                    <!-- <div class="ivu-table-cell">可上传多个文件
                      <template>
                        <Upload multiple action="//jsonplaceholder.typicode.com/posts/" style="display:inline-block;">
                          <Button icon="ios-cloud-upload-outline" style="color:#2996ff">上传</Button>
                        </Upload>
                      </template>
                    </div> -->
                  </td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>
      </div>
      <div class="but">
        <Row style="margin-top: 10px">
          <Col span="12">
          <PrintButton v-if="flag" Url="print/printAuditForm" :Project_id="info.Id || $route.query.ProjectId" Atype="二审"></PrintButton>
          </Col>
          <Col span="12" style="text-align:right;">
          <Button type="primary" v-show="isShow" @click="submit('')" :disabled='!stats'>保存</Button>
          <Button type="primary" v-show="isShow" style="margin:0 5px" @click="submit(1)" :disabled='!stats'>提交二审</Button>
          <!-- <Button type="primary" v-show="isShow" :disabled="stats" @click="submit(2)">提交初审</Button> -->
          </Col>
        </Row>
      </div>
      <StepBar :status="4" :viewOnly='viewOnly'/>
    </Card>
  </div>
</template>

<script>
  import libs from "../../../libs/uploads";
  export default {
    data() {
      return {
        shishi:'',
        xiazai:'下载',
        imgUpload: {
          Id: this.$route.query.ProjectId,
          Key: 'ImgUrl'
        },
        allIndex: "",
        stats: true,
        statsType: '',
        addBtton: true,
        flag: false,
        info: {},
        tofStateOne: true,
        tofStateTwo: true,
        tofStateThr: true,
        tofStateFou: true,
        tofStateFiv: true,
        viewOnlyState: false,
        viewOnly: true,
        xzRy: [],
        columns1: [{
          align: "center",
          title: "二审修改清单",
          children: [{
              type: "index",
              title: "序号",
              width: 80,
              align: "center",
              key: "num"
            },
            {
              title: "二审意见",
              key: "AuditOpinion",
              align: "center"
            },
            {
              title: "修改说明",
              key: "ReviseExplain",
              align: "center",
              render: (h, {
                row,
                index
              }) => {
                return h('Input', {
                  props: {
                    type: 'textarea',
                    autosize: true,
                    value: row.ReviseExplain,
                    disabled: this.viewOnlyState
                  },
                  on: {
                    'on-blur': (val) => {
                      this.info.data[index].ReviseExplain = val.target.value
                      this.info.data[index].ChangeTime = this.$moment().format('YYYY-MM-DD HH:mm:ss')
                    }
                  }
                })
              }
            }
          ]
        }],
        isShow: true,
        ProjectEnvOverview: '',
        proType: '',
        noUpload: '',
        outTime: 0,
      };
    },
    mounted() {
      // 获取当前信息
      this.getProject(this.$route.query.ProjectId, this.$route.query.Atype)
      this.getType(this.$route.query.ProjectId)
      if(this.$route.query.viewOnly == 1){
        this.viewOnlyState = true
        this.isShow = false
      }else{
        this.viewOnly = false
      }
      // 获取上传地址
      this.shishi = libs.uploads();
    },
    methods: {
      getIndex(data, index, event) {
        this.allIndex = index;
      },
      inputAdd(type) {
        this.info.data.push({})
      },
      inputRemove(type) {
        this.info.data.splice(this.allIndex, 1);
      },
      getType(Id){
        this.$post("ProjectInfo.findProjectByID",{
          Id
        }).then((res)=>{
          var n = res.info.PerjectType
          this.proType = res.info.PerjectType
            switch(true)
            { 
              case n == 6:
                this.tofStateOne = false;
                break;
              case n == 7:
                this.tofStateOne = false;
                this.tofStateTwo = false;
                break
              case n == 8:
                this.tofStateOne = false;
                this.tofStateTwo = false;
                this.tofStateThr = false;
                break;
              case n == 9:
                this.tofStateOne = false;
                this.tofStateTwo = false;
                this.tofStateThr = false;
                this.tofStateFou = false;
                break;
              case n >=10:
                this.tofStateOne = false;
                this.tofStateTwo = false;
                this.tofStateThr = false;
                this.tofStateFou = false;
                this.tofStateFiv = false;
                break;
              default:
                this.tofStateOne = true;
                this.tofStateTwo = true;
                this.tofStateThr = true;
                this.tofStateFou = true;
                this.tofStateFiv = true;
                break;
            }
        })
      },
      /* 获取当前创建项目 */
      getProject(ProjectId, Atype) {
        // axios.get('/audit/findByProject2', {
        this.$post('audit.findByProject3', {
            ProjectId,
            Atype
        }).then(res => {
          this.getDownInfo()
          this.info = res
          console.log(res)
          this.info.Modifier = parseInt(this.info.Modifier)
          // 查看当前项目状态
          this.getDoaudit(this.info.Level)
          this.flag = true
          if (this.info.data.length != 0) {
            this.addBtton = !this.addBtton
          }
          this.getXgry()
          localStorage.projectType = 5
          this.$post('audit.isAuditUp', {
            Id: ProjectId,
            AuditType: '二审'
          }).then(response => {
            this.statsType = response.Stats
            if(this.statsType == "已审核" && this.proType == '7') {
              this.imgUpload.Name = '初审' + 1
            } else if(this.statsType == '未提交') {
              
              if(this.info.data.length > 0){
                var number = this.info.ChangeTime.length + 2
                this.imgUpload.Name = '二审' + number
              } else {
                this.imgUpload.Name = '二审' + 1
              }
            }else{
              if(this.statsType == "已审核"){
                this.noUpload = '1'
              }else{
                this.noUpload = '2'
              }
            }
          })
        })
      },
      upLoadFun(){
        this.$refs.uploadButton.clearFiles()
        if(localStorage.getItem('userOrg') == 7){
          this.$Message.warning('档案员不可进行此操作')
          return false
        } else if(this.noUpload == '1'){
          this.$Message.warning('审核已结束，无法上传文件')
          return false
        }else if(this.noUpload == '2'){
          this.$Message.warning('审核已开始，无法上传文件')
          return false
        }
      },
      getDownInfo(){
        axios.post("wechatpublic/findImg",{
          Id:this.$route.query.ProjectId
        }).then((res)=>{
          if(this.info.Stats == '审核中'){
            this.ProjectEnvOverview =''
          }else{
            for(var i=0;i<res.data.length;i++){
              if(res.data[i].Status == '二审'){
                this.ProjectEnvOverview = res.data[i].ImgUrl
                this.xiazai = decodeURIComponent(this.ProjectEnvOverview.split('/')[5].substring(5,this.ProjectEnvOverview.split('/')[5].lastIndexOf('?')))
              }
            }
          }
        })
      },
      getXgry() {
        axios.get('/audit/findStaff', {
          params: {
            ProjectId: this.$route.query.ProjectId
          }
        }).then(res => {
          this.xzRy = res.data
        })
      },
      chengeXgry(value) {
        axios.post('/assignjobs/setModifier', {
            ProjectId: this.$route.query.ProjectId,
            Atype: "二审",
            Modifier: value.value
        }).then(res => {
          console.log(res)
          let title = res._MESSAGE
          if (res._STATUS == 1) {
            this.$Modal.success({
              title
            })
          } else {
            this.$Modal.error({
              title
            })
          }
        })
      },
      // 保存 save / 提交 submit
      submit(type) {
        let AuditReplyLists = this.info.data
        for(var audit = 0;audit<AuditReplyLists.length;audit++ ){
          if(!AuditReplyLists[audit].ReviseExplain){
            this.$Modal.error({
              title: '修改说明不能为空'
            })
            return
          }
        }
        let query = this.query
        let info = this.info
        info.BtnWay = type == 'save' ? 1 : 2
        info.PerjectType = type == 'save' ? 7 : 7
        console.log(this.info)
        if (type == 2) {
          axios.get('/projectInformation/saveOrUpdateProject', {
            params: {
              BtnWay: 2,
              PerjectType: 7,
              id: this.$route.query.ProjectId
            }
          }).then(res => {
            console.log(res)
            let title = res.MESSAGE
            if (res._STATUS == 1) {
              this.$router.push({
                name: 'firstTrial',
                query: {
                  ProjectId: this.$route.query.ProjectId,
                  Atype: 2
                }
              })
            } else {
              this.$Modal.error({
                title
              })
            }
          })
        } else {
          if(this.outTime == 0){
            this.outTime = 1
            setTimeout(()=>{this.outTime = 0},5000)
          let AuditReplyLists = this.info.data
          if (AuditReplyLists.length == 0) {
            axios.get('/audit/isAudit', {
              params: {
                ProjectId: this.$route.query.ProjectId,
                Atype: this.$route.query.Atype,
                Save: type
              }
            }).then(res => {
              if(res._MESSAGE == "审核已开始"){}else{
                social({
                  method: 'post',
                  url: '/info/notice',
                  params: {
                    content: info.ProjectName +'项目已提交二审，请尽快进行审核！',
                    app_name: '环评管控系统',
                    app: 'eao',
                    type: info.Auditor + '',
                    uid: -1
                  },
                })
              }
              let title = res._MESSAGE
              if (res._STATUS == 1) {
                this.$Modal.success({
                  title: title 
                })
                // 成功后跳转到正在进行中
                this.$router.push({
                  name: 'underway'
                })
              } else {
                this.$Modal.error({
                  title:res._MESSAGE
                })
              }
            })
          } else {
              axios.get('/audit/isAudit', {
                params: {
                  ProjectId: this.$route.query.ProjectId,
                  Atype: this.$route.query.Atype,
                  Save: type
                }
              }).then(res => {
                let title = res._MESSAGE
                if (res._STATUS == 1) {
                  axios.post('/auditreply/saveOrUpdateM', {
                    AuditReplyLists: JSON.stringify(AuditReplyLists),
                    ProjectId: this.$route.query.ProjectId,
                    Atype: this.$route.query.Atype,
                    Aoc: '3',
                    Save: type
                  }).then(res => {
                      social({
                        method: 'post',
                        url: '/info/notice',
                        params: {
                          content: info.ProjectName +'项目已提交二审，请尽快进行审核！',
                          app_name: '环评管控系统',
                          app: 'eao',
                          type: info.Auditor + '',
                          uid: -1
                        },
                        }).then((res)=>{
                          console.log(res)
                        }).catch((err)=>{
                          console.log(err)
                      })
                      this.$Modal.success({
                        title: title 
                      })
                  }).catch(err => {
                  })
                  this.$Modal.success({
                    title: title 
                  })
                  // 成功后跳转到正在进行中
                  this.$router.push({
                    name: 'underway'
                  })
                } else {
                  this.$Modal.error({
                    title
                  })
                }
              })
            }
          }else{
            console.log('别点了老铁')
          }
        }
      },
      getDoaudit(type) {
        axios.get('/audit/doaudit', {
          params: {
            ProjectLevel: type,
            ProjectId: this.$route.query.ProjectId
          }
        }).then(res => {
          if(localStorage.getItem('userOrg') == 7){
            this.stats = false;
          } else {
            this.stats = res.Stats == '1' ? true : false
          }
        })
      },
      firstInstance(){              //一审
        this.$router.push({name: 'firstInstance',query: { ProjectId: this.$route.query.ProjectId,Atype: 0,viewOnly: this.viewOnly?1:0}})
      },
      goReturn(){
        this.$router.push({name: 'reportReview',query: {ProjectId: this.$route.query.ProjectId,Atype: 1,viewOnly: this.viewOnly?1:0}})
      },
      firstTrial(){
        this.$router.push({name: 'firstTrial',query: {ProjectId: this.$route.query.ProjectId,Atype: 2,viewOnly: this.viewOnly?1:0}})
      },
      reExamine(){
        this.$router.push({name: 'reExamine',query: {ProjectId: this.$route.query.ProjectId,Atype: 3,viewOnly: this.viewOnly?1:0}}) 
      },
      lastInstance(){
        this.$router.push({name: 'lastInstance',query: {ProjectId: this.$route.query.ProjectId,Atype: 4,viewOnly: this.viewOnly?1:0}})
      },
    }
  };
</script>

<style lang="less" scoped>
  /deep/ textarea.ivu-input {
    font-size: 12px;
  }
</style>